﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.SessionState;
using BackEnd.Common;
using BackEnd.Models;
using BusinessLayer.WorldArtPrice;
using BusinessLayer.WorldArtPrice.Entities;
using CommandLib;

namespace BackEnd.Controllers
{
    public class ImageWhatWeDoController : Controller
    {
        //
        // GET: /ImageWhatWeDo/
        [CustomAuthorize(Action = Constants.AdminActions.CreateImageWhatWeDo)]
        public ActionResult Index()
        {
            ImageWhatWeDoModel imageWhatWeDoModel = new ImageWhatWeDoModel();
            ImageWhatWeDo imageWhatWeDo=new ImageWhatWeDoBL().GetTop();
            if (imageWhatWeDo != null)
            {
                imageWhatWeDoModel.ImageArtist = imageWhatWeDo.ImageArtist;
                imageWhatWeDoModel.RedirectUrlArtist = imageWhatWeDo.RedirectUrlArtist;
                imageWhatWeDoModel.ImageArtPrice = imageWhatWeDo.ImageArtPrice;
                imageWhatWeDoModel.RedirectUrlArtPrice = imageWhatWeDo.RedirectUrlArtPrice;
                imageWhatWeDoModel.ImageAuctionCalendar = imageWhatWeDo.ImageAuctionCalendar;
                imageWhatWeDoModel.RedirectUrlAuctionCalendar = imageWhatWeDo.RedirectUrlAuctionCalendar;
            }
            return View(imageWhatWeDoModel);
        }

        [HttpPost]
        public JsonResult Create(string imageArtist, string redirectUrlArtist, string imageArtPrice, string redirectUrlArtPrice, string imageAuctionCalendar, string redirectUrlAuctionCalendar)
        {
            string user = (string)Session[Common.Constants.SessionKeys.UserInfo];
            string permissionActions = (string)Session[Common.Constants.SessionKeys.PermissionActions];
            bool hasPermission = false;
            if (String.IsNullOrEmpty(user))
            {
                return Json(-1, JsonRequestBehavior.AllowGet);
            }

            hasPermission = permissionActions != null && permissionActions.Contains(Constants.AdminActions.CreateImageWhatWeDo);
            if (hasPermission == false)
            {
                return Json(-2, JsonRequestBehavior.AllowGet);
            }

            new ImageWhatWeDoBL().Insert(new ImageWhatWeDo { 
                                                ImageArtist = imageArtist, 
                                                ImageArtistUrl = Config.FolderImageWhatWeDo,
                                                RedirectUrlArtist = string.IsNullOrEmpty(redirectUrlArtist) ? string.Empty : redirectUrlArtist,
                                                ImageArtPrice = imageArtPrice, 
                                                ImageArtPriceUrl = Config.FolderImageWhatWeDo, 
                                                RedirectUrlArtPrice = string.IsNullOrEmpty(redirectUrlArtPrice) ? string.Empty: redirectUrlArtPrice,
                                                ImageAuctionCalendar = imageAuctionCalendar, 
                                                ImageAuctionCalendarUrl = Config.FolderImageWhatWeDo,
                                                RedirectUrlAuctionCalendar = string.IsNullOrEmpty(redirectUrlAuctionCalendar) ? string.Empty : redirectUrlAuctionCalendar });
                
            return Json(1, JsonRequestBehavior.AllowGet);
                       
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult UploadFile()
        {
            string imageName = string.Empty;
            string fileName = string.Empty;
            int errorCode = 100;
            try
            {
                var files = Request.Files;
                if (files.Count > 0)
                {
                    if (!Directory.Exists(Config.FolderImageWhatWeDo))
                    {
                        Directory.CreateDirectory(Config.FolderImageWhatWeDo);
                    }
                    for (int i = 0; i < files.Count; i++)
                    {
                        HttpPostedFileBase file = files[i];
                        imageName = file.FileName;
                        var lastIndexOfDot = imageName.LastIndexOf(".");
                        if (lastIndexOfDot > -1)
                        {
                            fileName = string.Format("{0}_{1}{2}", file.FileName.Substring(0, lastIndexOfDot - 1),
                                DateTime.Now.Ticks.ToString(),
                                file.FileName.Substring(lastIndexOfDot));
                            string filePath = Path.Combine(
                                Config.FolderImageWhatWeDo,
                                fileName);
                            file.SaveAs(filePath);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                errorCode = -999;
            }
            return Json(new { errorCode, fileName }, JsonRequestBehavior.AllowGet);
        }
    }
}
